/**
 * Copyright (C) 2022-2023
 * All rights reserved, Designed By www.tonetn.com
 * 注意：
 * 本软件为www.tonetn.com开发研制，未经购买不得使用
 * 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 * 一经发现盗用、分享等行为，将追究法律责任，后果自负
 */
package com.xunyin.modules.customer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.xunyin.common.service.impl.BaseServiceImpl;
import com.xunyin.domain.PageResult;
import com.xunyin.dozer.service.IGenerator;
import com.xunyin.modules.customer.domain.YxStoreCustomer;
import com.xunyin.modules.customer.service.YxStoreCustomerService;
import com.xunyin.modules.customer.service.dto.YxStoreCustomerDto;
import com.xunyin.modules.customer.service.dto.YxStoreCustomerQueryCriteria;
import com.xunyin.modules.customer.service.mapper.YxStoreCustomerMapper;
import com.xunyin.utils.FileUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;

/**
 * @author Bug
 * @date 2020-12-10
 */
@Slf4j
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "yxStoreCustomer")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxStoreCustomerServiceImpl extends BaseServiceImpl<YxStoreCustomerMapper, YxStoreCustomer> implements YxStoreCustomerService {

    private final IGenerator generator;

    @Override
    //@Cacheable
    public PageResult<YxStoreCustomerDto> queryAll(YxStoreCustomerQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<YxStoreCustomer> page = new PageInfo<>(queryAll(criteria));
        return generator.convertPageInfo(page, YxStoreCustomerDto.class);
    }


    @Override
    //@Cacheable
    public List<YxStoreCustomer> queryAll(YxStoreCustomerQueryCriteria criteria) {
/*        log.info("YxStoreCustomerQueryCriteria-getStoreId:"+criteria.getStoreId());
        log.info("YxStoreCustomerQueryCriteria-getNickName:"+criteria.getNickName());
        log.info("YxStoreCustomerQueryCriteria-getRemark:"+criteria.getRemark());*/
        LambdaQueryWrapper<YxStoreCustomer> wrapper = new LambdaQueryWrapper<>();
        if (criteria.getStoreId() != null) {
            wrapper.eq(YxStoreCustomer::getStoreId, criteria.getStoreId());
        }
        if (criteria.getNickName()  != null && !criteria.getNickName() .equals("")) {
            wrapper.eq(YxStoreCustomer::getNickName, criteria.getNickName());
        }
        if (criteria.getRemark() != null && !criteria.getRemark() .equals("")) {
            wrapper.eq(YxStoreCustomer::getRemark, criteria.getRemark());
        }
        List<YxStoreCustomer> list = baseMapper.selectList(wrapper);
        return list;
        //return baseMapper.selectList(QueryHelpPlus.getPredicate(YxStoreCustomer.class, criteria));
    }


    @Override
    public void download(List<YxStoreCustomerDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YxStoreCustomerDto yxStoreCustomer : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("用户昵称", yxStoreCustomer.getNickName());
            map.put("openId", yxStoreCustomer.getOpenId());
            map.put("备注", yxStoreCustomer.getRemark());
            map.put("添加时间", yxStoreCustomer.getCreateTime());
            map.put("修改时间", yxStoreCustomer.getUpdateTime());
            map.put(" isDel", yxStoreCustomer.getIsDel());
            map.put("是否启用", yxStoreCustomer.getIsEnable());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
